package vip.web3.common.utils;


import javax.mail.*;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class SendMailUtil {

	public static boolean sendMail(String code, String email) throws AddressException, MessagingException {
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.host", "smtpdm.aliyun.com");
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.port", 465);//其值为465
		props.put("mail.smtp.port", 465);
		props.put("mail.smtp.ssl.enable", true);// 设定用SSL方式
		Session session = Session.getDefaultInstance(props,
				//返回验证信息的内部类
				new Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						//发件人的用户名和smtp密码，用于验证身份
						return new PasswordAuthentication("@.vip", "123");
					}
				}
		);
		session.setDebug(false);
		Message msg = new MimeMessage(session);
		try {
			msg.setFrom(new InternetAddress("@.vip"));
			msg.setSubject("邮件验证码");
			//一个收件人
			msg.setRecipients(RecipientType.TO, InternetAddress.parse(email));
			msg.setContent("【火蚁 Fire Ant Pool】您的验证码为: <span style='color:red'>" + code + "</span> ，5分钟内有效，请谨慎保管。", "text/html;charset=gbk");
			//没有指定收件人 会在msg里面去找
			Transport.send(msg);
			return true;
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		return false;
	}

	public static boolean sendMail_(String code, String email) throws AddressException, MessagingException {
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.host", "smtpdm.aliyun.com");
		props.put("mail.smtp..class", "javax.net.ssl.");
		props.put("mail.smtp..port", 465);//其值为465
		props.put("mail.smtp.port", 465);
		props.put("mail.smtp.ssl.enable", "true");// 设定用SSL方式
		Session session = Session.getDefaultInstance(props,
				//返回验证信息的内部类
				new Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						//发件人的用户名和smtp密码，用于验证身份
						return new PasswordAuthentication("@.com", "");
					}
				}
		);
		session.setDebug(false);
		Message msg = new MimeMessage(session);
		try {
			msg.setFrom(new InternetAddress("@.com"));
			msg.setSubject("邮件验证码");
			//一个收件人
			msg.setRecipients(RecipientType.TO, InternetAddress.parse(email));
			msg.setContent("您的验证码为: <span style='color:red'>" + code + "</span>, 5分钟内有效，请谨慎保管。", "text/html;charset=gbk");
			//没有指定收件人 会在msg里面去找
			Transport.send(msg);
			return true;
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		return false;
	}

	public static void main(String[] args) {
		try{
			boolean b = sendMail("850225", "@qq.com");
		}catch (Exception ex){
			ex.printStackTrace();
		}
	}

}